package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AdapterBannerItemBinding;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.swgk.core.viewpager.CarouselPagerAdapter;
import com.swgk.core.viewpager.CarouselViewPager;

import java.util.List;

public class BannerAdapter extends CarouselPagerAdapter {
    private List<BannerEntity> bannerEntityList;
    private Context context;
    private onPagerItemClickListener mItemClickListener;
    private int variableId;

    public BannerAdapter(CarouselViewPager viewPager,
                         List<BannerEntity> bannerEntityList, Context context, int variableId) {
        super(viewPager);
        this.bannerEntityList = bannerEntityList;
        this.context = context;
        this.variableId = variableId;
    }

    public void setBannerEntityList(List<BannerEntity> bannerEntityList) {
        this.bannerEntityList = bannerEntityList;
        this.notifyDataSetChanged();
    }

    @Override
    public int getRealDataCount() {
        int result = 0;
        if (bannerEntityList != null) {
            result = bannerEntityList.size();
        }
        return result;
    }

    public void setOnPagerItemClickListener(onPagerItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public Object instantiateRealItem(ViewGroup container, final int position) {
        AdapterBannerItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_banner_item, null, false);
        binding.setVariable(variableId, bannerEntityList.get(position));


        String imgUrl = bannerEntityList.get(position).getImgUrl();
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(R.drawable.banner_default_bg)
                .error(R.drawable.banner_default_bg)
                .centerCrop();
        Glide.with(context)
                .load(imgUrl)
                .apply(options)
                .into(binding.headImg);

        container.addView(binding.getRoot(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return binding.getRoot();
    }

    public interface onPagerItemClickListener {
        void onPagerItemClicked(String bannerEntity);
    }
}
